//package com.mine.androidlearn.api.proxy;//package com.mine.learnone.common.proxy;
//
//import android.content.Context;
//import android.text.TextUtils;
//
//import com.cloud.api.ApiWrapper;
//import com.cloud.api.GlobalVariables;
//import com.cloud.api.IGlobalManager;
//import com.cloud.api.bean.LoginInfo;
//import com.cloud.api.bean.UserInfo;
//import com.cloud.api.constant.MobileType;
//import com.cloud.api.constant.UserType;
//import com.cloud.api.exception.TokenInvalidException;
//import com.hikvision.common.util.DeviceUtils;
//import com.mine.learnone.api.IGlobalManager;
//import com.mine.learnone.common.exception.TokenInvalidException;
//
//import java.lang.annotation.Annotation;
//import java.lang.reflect.InvocationHandler;
//import java.lang.reflect.InvocationTargetException;
//import java.lang.reflect.Method;
//
//import retrofit2.http.Field;
//import rx.Observable;
//import rx.functions.Func1;
//
//
///**
// * Created by zhangjiajie on 2017/2/8.
// */
//public class ProxyHandler implements InvocationHandler {
//
//    private final static String TOKEN = "token"; // 即token
//
//    private final Context mContext;
//    private boolean mIsTokenNeedRefresh;
//
//    private Object mProxyObject;
//    private IGlobalManager mGlobalManager;
//
//    public ProxyHandler(Context context, Object proxyObject, IGlobalManager globalManager) {
//        mProxyObject = proxyObject;
//        mGlobalManager = globalManager;
//        mContext = context;
//    }
//
//    @Override
//    public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
//        return Observable.just(null).flatMap(new Func1<Object, Observable<?>>() {
//            @Override
//            public Observable<?> call(Object o) {
//                try {
//                    if (mIsTokenNeedRefresh) {
//                        updateMethodToken(method, args);
//                    }
//                    return (Observable<?>) method.invoke(mProxyObject, args);
//                } catch (InvocationTargetException e) {
//                    e.printStackTrace();
//                } catch (IllegalAccessException e) {
//                    e.printStackTrace();
//                }
//                return null;
//            }
//        }).retryWhen(new Func1<Observable<? extends Throwable>, Observable<?>>() {
//            @Override
//            public Observable<?> call(Observable<? extends Throwable> observable) {
//                return observable.flatMap(new Func1<Throwable, Observable<?>>() {
//                    @Override
//                    public Observable<?> call(Throwable throwable) {
//                        if (throwable instanceof TokenInvalidException) {
//                            return refreshTokenWhenTokenInvalid();
//                        }
//                        return Observable.error(throwable);
//                    }
//                });
//            }
//        });
//    }
//
//    /**
//     * 重新登录以更新token值.
//     *
//     * @return Observable
//     */
////    private Observable<?> refreshTokenWhenTokenInvalid() {
////        synchronized (ProxyHandler.class) {
////            UserInfo userInfo = GlobalVariables.getInstance(mContext).getUserInfo();
////            String account = userInfo.getUserName();
////            final String password = userInfo.getPassword();
////            final String locateCity;
////            final int userType = userInfo.getUserType();
////            // 驾驶人版本需要传城市，商户版本不需要传城市
////            if (userType == UserType.DRIVER) {
////                locateCity = GlobalVariables.getInstance(mContext).getLocateCity();
////            } else {
////                locateCity = "";
////            }
////            Observable<?> observable = ApiWrapper.getInstance(mContext).refreshToken(account, password, locateCity,
////                    userType, MobileType.ANDROID, DeviceUtils.getDeviceId(mContext), DeviceUtils.getPhoneBrand()
////                            + " " + DeviceUtils.getPhoneModel()).flatMap(new Func1<LoginInfo, Observable<?>>() {
////                @Override
////                public Observable<?> call(LoginInfo loginInfo) {
////                    mIsTokenNeedRefresh = true;
////                    loginInfo.getUserInfo().setPassword(password);
////                    loginInfo.getUserInfo().setUserType(userType);
////                    GlobalVariables.getInstance(mContext).setLoginInfo(loginInfo);
////
////                    return Observable.just(true);
////                }
////            });
////            return observable;
////        }
////    }
//
//    /**
//     * 替换网络请求参数（token）
//     *
//     * @param method
//     * @param args
//     */
////    private void updateMethodToken(Method method, Object[] args) {
////        if (mIsTokenNeedRefresh && !TextUtils.isEmpty(GlobalVariables.getInstance(mContext).getToken())) {
////            Annotation[][] annotationsArray = method.getParameterAnnotations();
////            Annotation[] annotations;
////            if (annotationsArray != null && annotationsArray.length > 0) {
////                for (int i = 0; i < annotationsArray.length; i++) {
////                    annotations = annotationsArray[i];
////                    for (Annotation annotation : annotations) {
////                        if (annotation instanceof Field) {
////                            if (TOKEN.equals(((Field) annotation).value())) {
////                                args[i] = GlobalVariables.getInstance(mContext).getToken();
////                            }
////                        }
////                    }
////                }
////            }
////            mIsTokenNeedRefresh = false;
////        }
////    }
//}
